import java.util.Arrays;

public class TestHeap {
    public int[] elem;
    public int usedSize;
    public TestHeap(){
        this.elem=new int[9];
    }

    public void initElem(int[] arry){
        for (int i = 0; i <arry.length ; i++) {
            this.elem[i]=arry[i];
            usedSize++;
        }
    }
    public void creatHeap(){
        for(int parent=(usedSize-1-1)/2;parent>=0;parent--){
       shiftDown(parent,usedSize);
        }
    }
    public void shiftDown(int parent,int len){
        int child=2*parent+1;
        while(child<len){
            if(child+1<len&&elem[child]<elem[child+1]){
                child++;
            }
            if(elem[child]>elem[parent]){
                int tmp=elem[child];
                elem[child]=elem[parent];
                elem[parent]=tmp;
                parent=child;
                child=2*parent+1;
            }else{
                break;
            }

        }

    }

    public static void main(String[] args) {
        TestHeap heap = new TestHeap();
        int[] arr = new int[]{10,15,56,25,30,70};
        heap.initElem(arr);
        heap.creatHeap();
        System.out.println(Arrays.toString(heap.elem));

    }


}
